package com.idbk.chargestation.activity.assist;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.MainActivity;
import com.idbk.chargestation.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 * Created by zhx on 2017/12/14.
 */

public class ActivityGuidePage extends Activity {

    private static final int ZERO_PAGE = 0;
    private static final int FIRST_PAGE = 1;
    private static final int SECOND_PAGE = 2;
    private static final int THIRD_PAGE = 3;
    private Context mContext;
    private ViewPager mViewPager;
    private ImageView[] mImageViewList;
    private View mFirstView, mSecondView, mThirdView, mFourthView;
    private GuidePageAdapter mAdapter;
    private int mLastChecked = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide_page);

        initView();
    }

    private void initView() {
        mContext = ActivityGuidePage.this;
        initImageView();
        initPagerView();
        initAdapter();
        viewPageSetting();
    }

    private void initImageView() {
        mImageViewList = new ImageView[4];
        mImageViewList[ZERO_PAGE] = (ImageView) findViewById(R.id.first);
        mImageViewList[FIRST_PAGE] = (ImageView) findViewById(R.id.second);
        mImageViewList[SECOND_PAGE] = (ImageView) findViewById(R.id.third);
        mImageViewList[THIRD_PAGE] = (ImageView) findViewById(R.id.fourth);
    }

    private void initPagerView() {
        mViewPager = (ViewPager) findViewById(R.id.guide_viewpager);
        LayoutInflater inflater = LayoutInflater.from(ActivityGuidePage.this);
        mFirstView = inflater.inflate(R.layout.guide_page_fist, mViewPager, false);
        mSecondView = inflater.inflate(R.layout.guide_page_second, mViewPager, false);
        mThirdView = inflater.inflate(R.layout.guide_page_third, mViewPager, false);
        mFourthView = inflater.inflate(R.layout.guide_page_fourth, mViewPager, false);
        TextView texNext = (TextView) mFourthView.findViewById(R.id.experience);
        texNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityGuidePage.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initAdapter() {
        List<View> viewList = new ArrayList<View>();
        viewList.add(mFirstView);
        viewList.add(mSecondView);
        viewList.add(mThirdView);
        viewList.add(mFourthView);
        mAdapter = new GuidePageAdapter(viewList);
    }

    private void viewPageSetting() {
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == ZERO_PAGE) {
                    scrolledChange(ZERO_PAGE);

                } else if (position == FIRST_PAGE) {
                    scrolledChange(FIRST_PAGE);

                } else if (position == SECOND_PAGE) {
                    scrolledChange(SECOND_PAGE);

                } else if (position == THIRD_PAGE) {
                    scrolledChange(THIRD_PAGE);
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void scrolledChange(int currentCheck) {
        if (currentCheck != mLastChecked) {
            mImageViewList[currentCheck].setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.tint_black));
            mImageViewList[mLastChecked].setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.tint));
            mLastChecked = currentCheck;
        }
    }

    private class GuidePageAdapter extends PagerAdapter {

        private List<View> mViewList;

        GuidePageAdapter(List<View> viewList) {
            this.mViewList = viewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }
    }

}
